# 定义变量
IMAGE_NAME := webman-app
CONTAINER_NAME := webman-app
PORT_HTTP = 8787
# 默认目标
.PHONY: help
help:
	@echo "usage: make <option>"
	@echo "options and effects:"
	@echo "    help   	: 显示帮助"
	@echo "    build 	: 创建docker镜像"
	@echo "    run 		: 运行docker容器"
	@echo "    stop 	: 停止docker容器"
	@echo "    rm 		: 删除docker容器"
	@echo "    dev 		: 运行docker容器，将当前目录挂载到容器中进行开发,需要手动进行容器运行程序"
	@echo "    install 	: 运行docker容器，安装依赖到当前目录中"


# 构建 Docker 镜像
.PHONY: build
TAG = latest
build:
	@docker build -t $(IMAGE_NAME):$(TAG) .
	docker images |grep $(IMAGE_NAME)

# 启动容器
.PHONY: run
TAG = latest
run: rm build
	@docker run -d --name $(CONTAINER_NAME) -p $(PORT_HTTP):8787  $(IMAGE_NAME):$(TAG)

# 终止容器
.PHONY: stop
CONTAINER_ID = $(shell docker ps -q -a -f name=$(CONTAINER_NAME))
stop:
	@if [ -n "$(CONTAINER_ID)" ]; then \
		echo "存在正在运行的$(CONTAINER_NAME)容器:$(CONTAINER_ID),停止容器："; \
	  	docker stop $(CONTAINER_ID) ; \
	fi

# 删除容器
.PHONY: rm
CONTAINER_ID := $(shell docker ps -q -a -f name=$(CONTAINER_NAME))
rm: stop
	@if [ -n "$(CONTAINER_ID)" ]; then \
		docker rm $(CONTAINER_ID) ; \
	fi

# 启动开发环境
.PHONY: dev
TAG = latest
dev: rm build
	@docker run -d --name $(CONTAINER_NAME) -p $(PORT_HTTP):8787  -v $(CURDIR):/opt/www $(IMAGE_NAME):$(TAG)

# 安装项目
.PHONY: install
TAG = latest
install: rm build
	@docker run -d --name $(CONTAINER_NAME) -p $(PORT_HTTP):8787  -v $(CURDIR):/opt/www $(IMAGE_NAME):$(TAG) composer install -v
	@docker stop $(CONTAINER_NAME)
	@docker rm $(CONTAINER_NAME)
